<template>
  <div class="login">
    <!-- el-form表示表单组件， el-form-item表示表单组件中的每一项内容 -->
    <!-- model表示表单数据对象 -->
    <!-- 1.1 rules属性用于表单校验，校验规则需要在data中定义 -->
    <!-- 2.1 给el-form定义一个ref属性，用于js表单校验 -->
    <el-form :model="loginForm" :rules="myrules" ref="loginRef" status-icon class="container">
      <el-form-item>
        <div class="avatar">
          <img src="../assets/avatar.jpg" alt>
        </div>
      </el-form-item>
      <!-- 1.2 注意prop字段必须和loginForm对象中的字段名一致 -->
      <el-form-item prop="username">
        <!-- prefix-icon 属性用于给input框添加前缀图标 -->
        <el-input v-model="loginForm.username" autocomplete="off" prefix-icon="myicon myicon-user"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <!-- 给封装好的input组件绑定keydown事件的时候，必须加上.native修饰符 -->
        <el-input
          type="password"
          v-model="loginForm.password"
          autocomplete="off"
          prefix-icon="myicon myicon-key"
          @keydown.enter.native="handleLogin('loginRef')"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="login-btn" @click="handleLogin('loginRef')">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 引入api
import { login } from '@/api'
export default {
  data () {
    return {
      // 登录信息
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      //  登录验证
      myrules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    //   处理登录
    handleLogin (loginRef) {
      // 点击跳转到首页
      // 表单验证
      this.$refs[loginRef].validate((isPass) => {
        if (isPass) {
          // 发送请求
          login(this.loginForm)
            .then(res => {
              if (res.meta.status === 200) {
                console.log(res)
                //   设置请求头
                localStorage.setItem('mytoken', res.data.token)
                localStorage.setItem('username', res.data.username)
                this.$message.success('登录成功')
                //  跳转到首页
                setTimeout(() => {
                  this.$router.push({ name: 'home' })
                }, 1000)
              } else {
                this.$message.error(res.meta.msg)
              }
            })
        } else {
          // 提示信息
          this.$message.error('请输入用户名或密码')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #2f4050;

  .container {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 0px 40px 15px 40px;
    margin: 200px auto;
    background: white;
    .avatar {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>
